#define  _CRT_SECURE_NO_WARNINGS
#include "Tree.h"
#include "Queue.h"

void QueueNodeInit(Queue* pq)
{
	assert(pq);
	pq->phead = nullptr;
	pq->ptail = nullptr;
	pq->size = 0;
}

QueueNode* BuyNewNode(QDateType val)
{
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	assert(newnode);
	newnode->next = nullptr;
	newnode->data = val;
	return newnode;
}

void QueuePush(Queue* pq,QDateType val)
{
	assert(pq);
	QueueNode* newnode = BuyNewNode(val);
	if (pq->phead == nullptr)
	{
		pq->phead = newnode;
		pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = nullptr;
		pq->ptail = nullptr;
	}
	else
	{
		QueueNode* tmp = pq->phead->next;
		free(pq->phead);
		pq->phead = tmp;
	}
	pq->size--;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->phead == nullptr;
}

QDateType QueueTop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->data;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	//assert(pq->phead);
	if (pq->phead == pq->ptail)
	{
		free((pq->phead));
		pq->phead = nullptr;
	}
	else
	{
		QueueNode* tmp = pq->phead->next;
		free(pq->phead);
		pq->phead = tmp;
	}
	
}